package com.corgi.webservice.adapter;

import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title: XmlMapAdapter
 * @description: 适配器 处理CXF是处理不了的类型像Map、非javabean式的复合类
 * @author: dengmiao
 * @create: 2019-10-12 10:39
 **/
public class XmlMapAdapter extends XmlAdapter<ObjectMap, Map<Object, Object>> {

	@Override
	public Map<Object, Object> unmarshal(ObjectMap v) throws Exception {
		final Map<Object, Object> result = new HashMap<>(16);
		for (Entry entry : v.getEntries()) {
			result.put(entry.getKey(), entry.getValue());
		}
		return result;
	}

	@Override
	public ObjectMap marshal(Map<Object, Object> v) throws Exception {
		ObjectMap msm = new ObjectMap();
		List<Entry> eList = new ArrayList<>();
		for(Object key : v.keySet()) {
			Entry entry = new Entry();
			entry.setKey(key);
			entry.setValue(v.get(key));
			eList.add(entry);
		}
		msm.setEntries(eList);
		return msm;
	}

}
